package com.wondertek.poms.core.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gaozhengxing gaozhenghang@wondertek.com.cn
 * @date 2021/1/4 18:22
 * @description TODO
 */
public class FileIo {

    public static void fileCopy(String src, String tar, boolean isDelete) {
        File srcFile = new File(src);
        File tarFile = new File(tar);
        if (!srcFile.isDirectory()) {
            if (isDelete && tarFile.exists()) {
                tarFile.delete();
            } else if (!isDelete && tarFile.exists()) {
                return;
            }

            tarFile.getParentFile().mkdirs();
            FileInputStream is = null;
            FileOutputStream os = null;

            try {
                is = new FileInputStream(srcFile);
                os = new FileOutputStream(tarFile);
                byte[] buffer = new byte[8192];
                int bytesRead;
                while((bytesRead = is.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
            } catch (FileNotFoundException var24) {
                var24.printStackTrace();
            } catch (IOException var25) {
                var25.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (Exception var23) {
                    var23.printStackTrace();
                }

                try {
                    if (os != null) {
                        os.close();
                    }
                } catch (Exception var22) {
                    var22.printStackTrace();
                }

            }

        }
    }

    public static void deleteFile(String filePath) {
        String path = filePath.toString();
        File delFile = new File(path);
        delFile.delete();
    }

    public static String readFile(String inFileName) {
        try {
            File inFile = new File(inFileName);
            if (!inFile.exists()) {
                return "";
            } else {
                StringBuffer sb = new StringBuffer();
                FileReader inFileReader = new FileReader(inFile);
                BufferedReader inBufferedReader = new BufferedReader(inFileReader);
                String strLine = "";

                while((strLine = inBufferedReader.readLine()) != null) {
                    if (strLine != null && !"".equals(strLine)) {
                        strLine = strLine.replaceAll("'", "");
                        sb.append(strLine);
                        sb.append("\n");
                    }
                }

                return sb.toString();
            }
        } catch (Exception var6) {
            var6.printStackTrace();
            return "";
        }
    }

    public static String[] getLines(String fileName) {
        String str = null;
        FileReader fr = null;
        ArrayList lines = new ArrayList();

        try {
            fr = new FileReader(fileName);
            BufferedReader bfr = new BufferedReader(fr);

            while((str = bfr.readLine()) != null) {
                if (str.trim().length() > 0) {
                    lines.add(str);
                }
            }
        } catch (IOException var13) {
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException var12) {
            }

        }

        String[] result = new String[lines.size()];
        lines.toArray(result);
        return result;
    }

    public static String file2String(String filePath) {
        StringBuffer sb = new StringBuffer();
        InputStream inputStream = null;
        InputStreamReader inputStramReader = null;

        try {
            inputStream = new FileInputStream(new File(filePath));
            inputStramReader = new InputStreamReader(inputStream, "utf-8");
            boolean var4 = true;

            int ch;
            while((ch = inputStramReader.read()) != -1) {
                sb.append((char)ch);
            }
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            try {
                if (inputStramReader != null) {
                    inputStramReader.close();
                    inputStramReader = null;
                }

                if (inputStream != null) {
                    inputStream.close();
                    inputStream = null;
                }
            } catch (Exception var12) {
                var12.printStackTrace();
            }

        }

        return sb.toString();
    }

    public static List<String> readFileContantOY(FileInputStream fis, String split) {
        try {
            List<String> content = new ArrayList();
            String line = "";
            byte[] temp = new byte[1048576];
            int ti = 0;
            byte[] bs = new byte[256];

            for(int real = fis.read(bs); real > 0; real = fis.read(bs)) {
                for(int i = 0; i < bs.length; ++i) {
                    if (bs[i] == -128) {
                        line = line + new String(temp, 0, ti, "GBK");
                        line = line + split;
                        temp = new byte[1024];
                        ti = 0;
                    } else if (bs[i] == 13 && bs[i + 1] == 10) {
                        ++i;
                        line = line + new String(temp, 0, ti, "GBK");
                        temp = new byte[1024];
                        ti = 0;
                        if (line != null && line.length() > 0) {
                            content.add(line);
                        }

                        line = "";
                    } else {
                        temp[ti] = bs[i];
                        ++ti;
                    }
                }
            }

            return content;
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
    }
}
